import 'dart:convert';

import 'package:flutter_deer/database/beans/pay/create_order_entity.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/discount_resp_entity.g.dart';
import 'package:flutter_deer/util/input_formatter/money_util.dart';

export 'package:flutter_deer/generated/json/discount_resp_entity.g.dart';

@JsonSerializable()
class DiscountItem {

	DiscountItem();

	factory DiscountItem.fromJson(Map<String, dynamic> json) => $DiscountItemFromJson(json);

	bool available = false; // 储值卡是否可用
	bool canCompositePay = false; // 是否可混合支付
	bool canUseStoreCoupon = false; // 是否支持便利店优惠券
	String cardNo = '';
	int couponCount = 0; // 优惠券可用数量
	String couponDiscountAmount = ''; // 优惠券抵扣金额
	String couponId = ''; // 优惠券id
	List<CouponList> couponList = []; // 返回的优惠列表
	String couponName = '';
	String couponNotUseDesc = ''; // 不可用原因
	int couponSelectType = 0; // 1自己选 0推荐
	String discountAmount = ''; 			// 优惠金额
	List<Discount> discounts = []; 		// 优惠明细
	bool enableBuyExcludeOil = false; // 是否支持便利店支付 enableBuyExcludeOil
	String energyDiscountAmount = ''; // 能源商品优惠金额
	IntegralToCashDiscount? integralToCashDiscount; // 积分抵现明细
	bool maxDiscount = false; // 是否已达最大优惠
	String storeDiscountAmount = ''; // 便利店优惠金额
	List<StoreGoodsInfos> storeGoodsInfos = [];
	String memberAccountAmount = ''; // 账户余额-
	String memberAccountId = ''; // 储值卡ID
	String memberAccountType = ''; // 账户类型(个人：INDIVIDUAL、企业：ENTERPRISE)
	String belongMchType = '';
	String entityType = ''; // 账户类型(个人：INDIVIDUAL、企业：ENTERPRISE)
	String payTypeId = ''; // 支付方式id
	String payTypeName = ''; // 支付方式名称
	String showCardNo = ''; // 展示卡号
	String templateRechargeStatus = ''; // 正常:NORMAL, 不允许充值: FROBID_RECHARGE
	String payAmount = ''; // 最大可支付金额
	String payableAmount = ''; // 应付金额
	String unavailableCode = '';
	String unavailableMsg = '';
	int isSubRecharge = 0; // 是否支持子卡充值 是否支持子卡充值（0：否，1：是）

	Map<String, dynamic> toJson() => $DiscountItemToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}

	bool isEnterpriseCard() {
		return memberAccountType == 'ENTERPRISE';
	}

	String getCardNameOne() {
    if (belongMchType == 'group_mch') {
      return '集';
    }
    if (memberAccountType == 'INDIVIDUAL') {
      return '个';
    }
    if (memberAccountType == 'ENTERPRISE') {
      return '企';
    }
    if (memberAccountType == 'GIFT_CARD') {
      return '礼';
    }
    return '-';
  }
}

@JsonSerializable()
class CouponList {

	CouponList();

	String? couponAmount = '';
	String? couponId = '';
	String? couponName = '';
	String? couponType = '';
	List<CouponGoodsDetails>? goodsDetails = [];

	factory CouponList.fromJson(Map<String, dynamic> json) => $CouponListFromJson(json);

	Map<String, dynamic> toJson() => $CouponListToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}
}

@JsonSerializable()
class CouponGoodsDetails {

	CouponGoodsDetails();

	String? discountAmount = '';
	String? discountQuantity = '';
	String? goodsId = '';
	String? salesChannelGoodsId = '';
	String? useIntegral = '';

	factory CouponGoodsDetails.fromJson(Map<String, dynamic> json) => $CouponGoodsDetailsFromJson(json);

	Map<String, dynamic> toJson() => $CouponGoodsDetailsToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}
}

@JsonSerializable()
class IntegralToCashDiscount {

	IntegralToCashDiscount();

	bool? available = false;
	String? cashValue = '';
	String? discountAmount = '';
	String? integralValue = '';
	String? maxUseIntegral = '';
	String? memberIntegral = '';
	String? promotionsCode = '';
	String? promotionsId = '';
	String? reason = '';
	String? type = '';
	String? typeDesc = '';
	String? useIntegral = '';

	factory IntegralToCashDiscount.fromJson(Map<String, dynamic> json) => $IntegralToCashDiscountFromJson(json);

	Map<String, dynamic> toJson() => $IntegralToCashDiscountToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}

	// 计算最大抵扣金额
	String get maxAmount {
		return '$maxUseIntegral'.multiply('$cashValue').divide('$integralValue');
	}

	// 获取当前使用的积分
	String get useIntegralAmount {
		return useIntegral == '0' ? '$maxUseIntegral' : '$useIntegral';
	}
}
